import {
	Decimal
} from '@/static/decimal.js'

Decimal.set({
	precision: 6
});

export function checkIncData(incData) {
	let {
		money,
		perc,
		cnt,
		calcModeCur,
		percModeCur,
		calcModeCurText,
		minMoney
	} = incData
	
	if(calcModeCur == 0) { //按本金
		minMoney = minMoney * 10
	}
	if(!isMoney(money) || money < minMoney){
		toast(`${calcModeCurText}必须大于${minMoney},最多两位小数`)
		return false;
	}
	if (!isMoney(perc) || perc >= 100) {
		toast("每日上涨/下跌必须在0至100之间,最多两位小数")
		return false;
	}
	if (cnt < 1) {
		toast("交易天数必须是大于1的数字")
		return false;
	}
	return true
}

export function checkTaxData(taxData) {
	let {
		taxModeCur,
		stamp,
		commi,
		minCommi,
		trans,
		minTrans
	} = taxData

	if (taxModeCur != 0) {
		if (!isMoney(stamp) || stamp >= 1000) {
			if (stamp !== '0') {
				toast("印花税费率必须在0至1000之间,最多两位小数")
				return false;
			}
		}
		
		if (!isMoney(commi) || commi >= 10000) {
			if (commi !== '0') {
				toast("佣金费率必须在0至10000之间,最多两位小数")
				return false;
			}
		}

		if (!isMoney(trans) || trans >= 10000) {
			if (trans !== '0') {
				toast("过户费费率必须在0至10000之间,最多两位小数")
				return false;
			}
		}
	}
	return true
}

export function calcTax(money, taxData, type) {
	let {
		taxModeCur,
		stamp,
		commi,
		minCommi,
		trans,
		minTrans
	} = taxData

	money = Decimal(money)
	stamp = Decimal(stamp)
	commi = Decimal(commi)
	minCommi = Decimal(minCommi)
	trans = Decimal(trans)
	minTrans = Decimal(minTrans)
	let staMoney = Decimal(0)
	if ('b' != type) { //买入不算印花税
		staMoney = money.mul(stamp.div(1000)) // 印花税费率，千分位
	}

	let comiMoney = money.mul(commi.div(10000)) // 佣金税费率，万分位
	let tranMoney = money.mul(trans.div(10000)) // 过户费费率，万分位
	if (comiMoney.lt(minCommi)) {
		comiMoney = minCommi
	}
	if (tranMoney.lt(minTrans)) {
		tranMoney = minTrans
	}

	//佣金、过户费双向收取
	let tax = staMoney.add(comiMoney).add(tranMoney)
	return tax.neg()
}

export function calcInc(incData, taxData) {
	let {
		money,
		perc,
		cnt,
		minMoney,
		percModeCur
	} = incData

	if (!checkIncData(incData)) {
		return []
	}

	if (!checkTaxData(taxData)) {
		return []
	}

	perc = Decimal(perc)
	if (percModeCur == 1) { //下跌
		perc = perc.negated()
	}
	let i = 0
	let incArr = []
	let taxModeCur = taxData.taxModeCur //0不计算 1每日计算 2首尾计算
	const firstMoney = Decimal(money) //总初始资金,不会改动
	let dayFirstMoney = firstMoney //日初始资金,每日变化
	let preTotalDayTax = Decimal(0) //前一天累计税费
	let preTotalPerc = Decimal(0) //前一天累计涨幅
	let preMult = Decimal(0) //前一天倍数
	while (++i <= cnt) {
		let dayMoney = dayFirstMoney
		if (dayMoney.lte(minMoney)) {
			break
		}

		let buyTax = 0
		if (taxModeCur == 1) { //每日计算
			buyTax = calcTax(dayMoney, taxData, 'b') //计算买入税费
		}
		if (taxModeCur == 2 && i == 1) { //首尾计算
			buyTax = calcTax(dayMoney, taxData, 'b') //计算买入税费
		}
		dayMoney = dayMoney.add(buyTax)
		if (dayMoney.lte(0)) {
			dayMoney = Decimal(0)
		}
		let dayPercMoney = dayMoney.mul(perc.div(100))
		dayMoney = dayMoney.add(dayPercMoney)

		let sellTax = 0
		if (taxModeCur == 1) {
			sellTax = calcTax(dayMoney, taxData, 's')
		}
		if (taxModeCur == 2 && (i == cnt || dayMoney.lte(10))) { //首尾计算
			sellTax = calcTax(dayMoney, taxData, 's')
		}

		dayMoney = dayMoney.add(sellTax)

		let dayTax = Decimal(buyTax).add(sellTax)
		let totalTax = dayTax.add(preTotalDayTax)
		let totalInc = dayMoney.sub(firstMoney)
		let dayInc = dayMoney.sub(dayFirstMoney)
		let dayPerc = dayInc.div(dayFirstMoney).mul(100)
		let totalPerc = totalInc.div(firstMoney).mul(100)
		let mult = totalInc.div(firstMoney).toFixed(1, Decimal.ROUND_DOWN)
		let upMult = Decimal(0)//涨N倍回本
		if(totalPerc < 0) {
			var a = Decimal(1)
			var b = Decimal(1).add(Decimal(totalPerc).div(100))
			var c = Decimal(1)
			upMult = a.div(b).sub(c)
		}
		incArr.push({
			'day': i, //天数
			'dayMoney': dayMoney, //每日交易后本金
			'dayFirstMoney': dayFirstMoney, // 每日交易前本金
			'buyTax': buyTax, //买入税费
			'sellTax': sellTax, //卖出税费
			'dayTax': dayTax, //每日税费（买入+卖出）
			'totalTax': totalTax, //累计税费（前一日+今日）
			'dayInc': dayInc, //每日收益
			'totalInc': totalInc, //累计收益（前一日+今日）
			'dayPerc': dayPerc, //每日涨跌幅
			'dayPercMoney': dayPercMoney, //每日涨跌额
			'preTotalPerc': preTotalPerc, //前一日累计涨幅
			'mult': mult, //涨幅换算成倍数
			'upMult': upMult, //涨N倍回本
			'preMult': preMult, //前一日倍数
			'totalPerc': totalPerc //累计涨幅
		})

		preTotalDayTax = totalTax
		preTotalPerc = totalPerc
		preMult = mult
		dayFirstMoney = dayMoney
	}
	return incArr
}

export function isMoney(value) {
	let money = value || -1
	// 最多只有两位小数的正则表达式
	let reg = /(^[1-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,2}$)/;

	if (!reg.test(value)) {
		return false
	}

	if (value <= 0) {
		return false
	}
	return true
}

export function toast(title, duration) {
	uni.showToast({
		title: title || '',
		icon: 'none',
		duration: duration || 1500
	})
}

export const LONG_COLOR = '#e54d42' //#e54d42 #c93c04 #299236 #39b54a

export function simplifyNum(number, dec, decType) {
	if(number == '' || number == undefined) {
		return 0
	}
	dec = dec || 2
	if (decType == 1) {
		decType = Decimal.ROUND_DOWN
	} else {
		decType = Decimal.ROUND_HALF_UP
	}
	number = Decimal(number)
	var str_num = Decimal(0)
	var unit = ''

	if (number.abs() >= 1E4 && number.abs() < 1E6) {
		str_num = number.div(1E4)
		unit = '万'
	} else if (number.abs() >= 1E6 && number.abs() < 1E7) {
		str_num = number.div(1E6)
		unit = '百万'
	} else if (number.abs() >= 1E7 && number.abs() < 1E8) {
		str_num = number.div(1E7)
		unit = '千万'
	} else if (number.abs() >= 1E8 && number.abs() < 1E10) {
		str_num = number.div(1E8)
		unit = '亿'
	} else if (number.abs() >= 1E10 && number.abs() < 1E11) {
		str_num = number.div(1E10)
		unit = '百亿'
	} else if (number.abs() >= 1E11 && number.abs() < 1E12) {
		str_num = number.div(1E11)
		unit = '千亿'
	} else if (number.abs() >= 1E12 && number.abs() < 1E13) {
		str_num = number.div(1E12)
		unit = '万亿'
	} else if (number.abs() >= 1E13) {
		if (number < 0) {
			unit = '-10万亿+'
		}
		unit = '10万亿+'
		return unit
	} else {
		str_num = number
		unit = ''
	}
	// return str_num.toFixed(dec, decType) + unit
	return str_num.toSignificantDigits(dec, decType) + unit
}